package com.liruo.treasureattic.common.util;

import org.junit.jupiter.api.Test;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.*;
 
public class MacUtil {
 
    public static void main(String[] args) {
        System.out.println(getCurrentIpLocalMac());
        System.out.println(getAllLocalMac());
    }

    @Test
    public void test1() {
        byte a = -90;
        int b = a;
        System.out.println(b);
        System.out.println(b & 0xFF);
        System.out.println(Integer.toBinaryString(b &0xff));
        System.out.println(Integer.toHexString(b & 0xFF));
        System.out.println(Integer.toHexString(a));
        System.out.println((byte)(b & 0xFF));
        System.out.println((byte)(a & 0xFF));

        byte x1 = 4;
        byte x2 = 2;
        byte x3 = (byte) (x1 / x2);
        System.out.println(x3);
    }
 
    /**
     * 获取当前所用ip的mac地址
     * @return
     */
    public static String getCurrentIpLocalMac(){
        InetAddress ia = null;
        try {
            ia = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
 
        byte[] mac = new byte[0];
        try {
            // NetworkInterface.getByInetAddress(ia) 根据ip信息获取网卡信息
            NetworkInterface byInetAddress = NetworkInterface.getByInetAddress(ia);
            mac = byInetAddress.getHardwareAddress();
        } catch (SocketException e) {
            e.printStackTrace();
        }
 
        StringBuffer sb = new StringBuffer("");
        for(int i=0; i<mac.length; i++) {
            if(i!=0) {
                sb.append("-");
            }
            //字节转换为整数
            int temp = mac[i]&0xff;
            // 把无符号整数参数所表示的值转换成以十六进制表示的字符串
            String str = Integer.toHexString(temp);
            if(str.length()==1) {
                sb.append("0"+str);
            }else {
                sb.append(str);
            }
        }
 
        return sb.toString();
    }
 
    /**
     * 获取本地所有mac文件
     * @return
     */
    public static List<String> getAllLocalMac(){
        // 使用set集合，避免重复
        Set<String> macs = new HashSet<>();
 
        try {
            Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
            while (enumeration.hasMoreElements()) {
                StringBuffer stringBuffer = new StringBuffer();
                NetworkInterface networkInterface = enumeration.nextElement();
                if (networkInterface != null) {
                    byte[] bytes = networkInterface.getHardwareAddress();
                    if (bytes != null) {
                        for (int i = 0; i < bytes.length; i++) {
                            if (i != 0) {
                                stringBuffer.append("-");
                            }
                            // 字节转换为整数
                            int tmp = bytes[i] & 0xff;
                            // 把无符号整数参数所表示的值转换成以十六进制表示的字符串
                            String str = Integer.toHexString(tmp);
                            if (str.length() == 1) {
                                stringBuffer.append("0" + str);
                            } else {
                                stringBuffer.append(str);
                            }
                        }
                        String mac = stringBuffer.toString();
                        macs.add(mac);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // Set 转 List
        List<String> macList = new ArrayList<>(macs);
        return macList;
    }
 
}